package com.zzl.finance.data.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.finance.data.dto.*;
import com.zzl.finance.data.entity.FinanceInstitutyEntity;
import com.zzl.finance.data.service.FinanceInstitutyService;
import com.zzl.general.common.apidoc.annotation.*;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 金融机构
 *
 * @author wztsl
 * @date 2021-12-22 16:56:38
 */
@ApidocController("6金融机构")
@RestController
@RequestMapping("data/financeInstituty")
@Slf4j
public class FinanceInstitutyController extends WebBaseCtrl {
    @SuppressWarnings("all")
    @Autowired
    private FinanceInstitutyService financeInstitutyService;

    @GetMapping("/getFinanceInstituty")
    @ApidocOperation(description = "1融单填写-金融机构列表", operationId = "data/financeInstituty/getFinanceInstituty")
    @ApidocReturnList(genericTypeClass = FinanceInstitutyDto.class)
    public Result getFinanceInstituty(@ApidocParamsPage(name = "params",
            keyName = {
                    "institutyId",
                    "transferAmount",
                    "paymentMode"
            },
            keyDescription = {
                    "金融机构id",
                    "转让金额",
                    "付息方式 ：1 融资方付息 2 核心企业付息"
            },
            keyRequired = {
                    true,
                    true,
                    true
            }
    ) @RequestParam Map<String, Object> params) {
        try {
            return Result.ok().put("list", financeInstitutyService.getFinanceInstituty(params, this.getUser()));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApidocOperation(description = "2保存金融机构", operationId = "data/financeinstituty/save")
    @ApidocReturnR
    public Result save(@ApidocParam(name = "保存的金融机构对象值", required = true) @RequestBody @Validated FinanceInstitutyAddDto financeInstituty) {
        try {
            financeInstitutyService.saveFinanceInstituty(financeInstituty, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApidocOperation(description = "2分页查询金融机构数据", operationId = "data/financeinstituty/list")
    @ApidocReturnPage(genericTypeClass = FinanceInstitutyPageDto.class)
    public Result list(@ApidocParamsPage(name = "params"
            , keyName = {
            "instinctyName"
            , "accessProduct"
            , "page"
            , "order"
    }
            , keyDescription = {
            "资金方名称"
            , "接入产品，1：全部 2：反向保理、3：数字债权"
            , "排序类型：create_time（默认）"
            , "排序方式：desc 降序（默认）asc 升序"}
    ) @RequestParam Map<String, Object> params) {
        PageUtils page = financeInstitutyService.queryPage(params, this.getSession());
        return Result.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApidocOperation(description = "2通过id获取金融机构数据", operationId = "data/financeinstituty/info")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "financeInstituty", valueClass = FinanceInstitutyInfoDto.class)
    })
    public Result info(@ApidocParam(name = "金融机构id", required = true) @PathVariable("id") String id) {
        FinanceInstitutyInfoDto financeInstituty = null;
        try {
            financeInstituty = financeInstitutyService.getFinanceInstitutyById(id);
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
        return Result.ok().data(financeInstituty);
    }

    @PutMapping("/updateStatus/{id}")
    @ApidocOperation(description = "2冻结/启用", operationId = "data/financeinstituty/frozen")
    @ApidocReturnR
    public Result updateStatus(@ApidocParam(name = "id", description = "资金方id", required = true) @PathVariable("id") String id) {
        try {
            financeInstitutyService.updateStatusById(id, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 列表
     */
    @GetMapping("/getFinancingScope/{type}")
    @ApidocOperation(description = "2融资范围下拉框", operationId = "data/financeInstituty/list")
    @ApidocReturnList(genericTypeClass = FinanceInstitutyEntity.class)
    public Result getFinancingScope(@ApidocParam(name = "type", description = "接入产品，2：反向保理、3：数字债权", required = true) @PathVariable("type") Integer type) {
        return Result.ok().put("list", financeInstitutyService.getFinancingScope(type));
    }


    /**
     * 修改
     */
    @PutMapping("/update")
    @ApidocOperation(description = "修改金融机构", operationId = "data/financeinstituty/update")
    @ApidocReturnR
    public Result update(@ApidocParam(name = "修改的金融机构对象值", required = true) @RequestBody FinanceInstitutyUpdateDto financeInstituty) {
        try {
            financeInstitutyService.updateFinanceInstitutyById(financeInstituty, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApidocOperation(description = "删除金融机构", operationId = "data/financeinstituty/delete")
    @ApidocReturnR
    public Result delete(@ApidocParam(name = "删除的金融机构对象id集合", required = true) @RequestBody Integer[] ids) {
        financeInstitutyService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

    /**
     * 1 金融机构下拉框
     * 2 额度管理-金融机构下拉框
     */
    @GetMapping("/listAll")
    @ApidocOperation(description = "1 资方下拉框", operationId = "data/financeInstituty/list")
    @ApidocReturnList(genericTypeClass = FinanceInstitutyEntity.class)
    public Result listAll() {
        return Result.ok().put("list", financeInstitutyService.dropDownBox());
    }

}
